package com.haochang.chunkvod.app.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.text.TextUtils;

/**
 * @author qintao
 *         created at 2016/7/13 11:17
 *         时间转换
 */
public class TimeUtils
{
	private final static String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	private final static String DATE_FORMAT = "yyyy-MM-dd";
	private final static String HOURMIN_FORMAT = "HH:mm";

	/**
	 * 2016-06-25 21:25:00 截取时间，返回小时：分钟
	 *
	 * @param time
	 * @return
	 */
	public static String getMinutesString(String time)
	{
		try
		{
			if (!TextUtils.isEmpty(time))
			{
				return time.substring(11, 16);
			}
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return "";
	}

	public static String getMinutesString2(String time)
	{
		if (!TextUtils.isEmpty(time))
		{
			return time.substring(0, 5);
		}
		return "";
	}

	/**
	 * 2016-06-25 21:25:00 截取时间，返回年月日
	 *
	 * @param time
	 * @return
	 */
	public static String getDayString(String time)
	{
		if (!TextUtils.isEmpty(time))
		{
			return time.substring(0, 10);
		}
		return "";
	}

	/** *
	 * 获取指定日期是星期几
	 * @param time
	 * @return
	 */
	public static String getWeekOfDate(String time)
	{
		String[] weekOfDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
		SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT);
		Date date = null;
		try
		{
			date = sdf.parse(time);
		} catch (ParseException e)
		{
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		if (date != null)
		{
			calendar.setTime(date);
		}
		int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
		{
			w = 0;
		}
		return weekOfDays[w];
	}

	/**
	 * 获取当前时间
	 * @return
	 */
	public static String getNowTime()
	{
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(TIME_FORMAT);//可以方便地修改日期格式
		String time = dateFormat.format(now);
		return time;
	}

	/**
	 * 两个时间比较大小
	 * @param time1
	 * @param time2
	 * @param value 大多少 毫秒
	 * @return
	 */
	public static boolean compareTime(String time1, String time2, int value)
	{
		boolean flag = false;
		SimpleDateFormat dateFormat = new SimpleDateFormat(TIME_FORMAT);
		try
		{
			Date date1 = dateFormat.parse(time1);
			Date date2 = dateFormat.parse(time2);
			if (date1.getTime() - date2.getTime() > value)
			{
				flag = true;
			}
		} catch (ParseException e)
		{
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}

	/**
	 * 比较两个时间的大小
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static boolean compareTime(String time1, String time2)
	{
		boolean flag = false;
		time1 = getMinutesString(time1);
		time2 = getMinutesString(time2);
		SimpleDateFormat dateFormat = new SimpleDateFormat(HOURMIN_FORMAT);
		try
		{
			Date date1 = dateFormat.parse(time1);
			Date date2 = dateFormat.parse(time2);
			if (date1.getTime() > date2.getTime())
			{
				flag = true;
			}
		} catch (ParseException e)
		{
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}

	/**
	 * 自定义两个时间相加,只适用于本项目
	 *
	 * @param time
	 * @param hour
	 * @param minute
	 * @return
	 */
	public static String timePlus(String time, int hour, int minute)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(HOURMIN_FORMAT);
		try
		{
			time = getMinutesString(time);
			Date date = sdf.parse(time);
			int h = date.getHours();
			int m = date.getMinutes();
			m = m + minute;
			h = h + hour;
			if (m / 60 > 0)
			{
				h = h + m / 60;
				m = m % 60;
			}
			return h + ":" + m;
		} catch (ParseException e)
		{
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 自定义两个时间相加,只适用于本项目
	 *
	 * @param time
	 * @param hour
	 * @param minute
	 * @return
	 */
	public static String getNewTimeAfterTimePlus(String time, int hour, int minute)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT);
		try
		{
			Date date = sdf.parse(time);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.HOUR_OF_DAY, hour);
			cal.add(Calendar.MINUTE, minute);
			date = cal.getTime();
			String value = sdf.format(date);
			return value;
		} catch (ParseException e)
		{
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 两个时间相减,只适用于本项目
	 *
	 * @param time
	 * @param hour
	 * @param minute
	 * @return
	 */
	public static String timeMinus(String time, int hour, int minute)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(HOURMIN_FORMAT);
		try
		{
			time = getMinutesString(time);
			Date date = sdf.parse(time);
			int h = date.getHours();
			int m = date.getMinutes();
			Date date2 = sdf.parse(hour + ":" + minute);
			if (date.getTime() > date2.getTime())
			{
				h = h - 24;
			}
			m = minute - m;
			h = hour - h;
			if (m < 0)
			{
				m = m + 60;
				h = h - 1;
			}
			return h + ":" + m;
		} catch (ParseException e)
		{
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 获取两个时间之间的时间差(分)
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static int timeBetween(String time1, String time2)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT);
		int value = 0;
		try
		{
			if (!TextUtils.isEmpty(time1) && !TextUtils.isEmpty(time2))
			{
				Date date1 = sdf.parse(time1);
				Date date2 = sdf.parse(time2);
				value = (int) ((date1.getTime() - date2.getTime()) / (1000 * 60));
			}
		} catch (ParseException e)
		{
			e.printStackTrace();
		}
		return value;
	}

	/**
	 * 两个时间相减,只适用于本项目
	 *time1减time2
	 * @param time1
	 * @param time1
	 * @return
	 */
	public static String timeMinus(String time1, String time2)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT);
		try
		{
			Date date1 = sdf.parse(time1);
			Date date2 = sdf.parse(time2);
			long value = 0;
			if (date1.getTime() > date2.getTime())
			{
				value = date1.getTime() - date2.getTime();
			}
			int h = 0;
			int m = 0;
			if (value > 0)
			{
				h = (int) (value / (1000 * 60 * 60));
				m = (int) ((value - h * 1000 * 60 * 60) / (1000 * 60));
			}
			return h + ":" + m;
		} catch (ParseException e)
		{
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 两个时间相减,只适用于本项目，返回样式(XX小时XX分钟)
	 *time1减time2
	 * @param time1
	 * @param time1
	 * @return
	 */
	public static String timeMinusCn(String time1, String time2)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT);
		try
		{
			Date date1 = sdf.parse(time1);
			Date date2 = sdf.parse(time2);
			long value = 0;
			if (date1.getTime() > date2.getTime())
			{
				value = date1.getTime() - date2.getTime();
			}
			int h = 0;
			int m = 0;
			if (value > 0)
			{
				h = (int) (value / (1000 * 60 * 60));
				m = (int) ((value - h * 1000 * 60 * 60) / (1000 * 60));
			}
			return (h > 0 ? h + "小时" : "") + m + "分钟";
		} catch (ParseException e)
		{
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 两个时间相减,只适用于本项目，返回样式(2h+或者58m)
	 *time1减time2
	 * @param time1 结束时间
	 * @param time1 开始时间
	 * @return
	 */
	public static String timeMinusEn(String time1, String time2)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT);
		try
		{
			Date date1 = sdf.parse(time1);
			Date date2 = sdf.parse(time2);
			long value = 0;
			if (date1.getTime() > date2.getTime())
			{
				value = date1.getTime() - date2.getTime();
			}
			int h = 0;
			int m = 0;
			if (value > 0)
			{
				h = (int) (value / (1000 * 60 * 60));
				m = (int) ((value - h * 1000 * 60 * 60) / (1000 * 60));
			}
			if (h >= 1)
			{
				return h + "h+";
			} else if (m > 0)
			{
				return m + "m";
			}
			return "0:0";
		} catch (ParseException e)
		{
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 获取时间戳
	 */
	public static String getTimeStr()
	{
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String str = sdf.format(date);
		return str;
	}

	/**
	 * 将时间戳转换为字符串(hh:mm)
	 * @param time
	 * @return
	 */
	public static String getTimeString(long time)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
		return sdf.format(time);
	}

	/**
	 * 将时间戳转换为字符串(HH:mm)24小时制的
	 * @param timeStamp 时间戳
	 * @return 16:16
	 */
	public static String getTimeString24HR(long timeStamp)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
		return sdf.format(timeStamp);
	}

	/**
	 * 分钟数转换为小时
	 * @param min
	 * @return
	 */
	public static String minToHour(int min)
	{
		int hour = min / 60;
		int m = min % 60;
		return (hour > 0 ? hour + "小时" : "") + (m > 0 ? m + "分" : "");
	}

	/**
	 * 秒数转换为小时分钟
	 * @param mis
	 * @return
	 */
	public static String misToHour(int mis)
	{
		int min = mis / 60;
		int hour = min / 60;
		int m = min % 60;
		return (hour > 0 ? hour + "小时" : "") + (m > 0 ? m + "分" : "");
	}

	/**
	 * 秒数转换为小时分钟
	 * @param seconds 秒
	 * @return
	 */
	public static String misToHour2(int seconds)
	{
		int min = seconds / 60;
		int hour = min / 60;
		int m = hour == 0 ? min % 60 : Math.abs(min % 60);
		return (hour != 0 ? hour + "小时" : "") + m + "分钟";
	}
}
